package com.travel.server.manager.impl.command.message;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.message.UpdateMessageMsgRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.MessageDal;
import com.travel.server.manager.impl.enums.MessageExamStateEnum;
import com.travel.server.manager.impl.enums.MessageTemplateEnum;
import com.travel.server.manager.impl.sal.MessageSal;
import com.travel.server.manager.impl.sms.SmsSal;
import com.travel.server.manager.model.MessageTemplate;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018\11\15 0015 22:20
 * @Desc:
 */
@Component
@Slf4j
public class UpdateMessageMsgCommand extends AbstractCommand<UpdateMessageMsgRequest, EmptyResponse> {

    @Autowired
    private MessageDal messageDal;

    @Override
    protected void checkInput(UpdateMessageMsgRequest request) {
        if(StringUtils.isBlank(request.getType()) || StringUtils.isBlank(request.getName()) || StringUtils.isBlank(request.getContent()) || StringUtils.isBlank(request.getSign())){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(UpdateMessageMsgRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        Integer id = request.getId();
        MessageTemplate messageTemplate;
        if(id == null){
            messageTemplate = new MessageTemplate();
            messageTemplate.setCreateBy(userInfo.getName());
            messageTemplate.setCreateTime(now);
            messageTemplate.setState(CommonConstant.YES);
            messageTemplate.setExamState(MessageExamStateEnum.EXAM_ING.getState());
            messageTemplate.setMessageType(MessageTemplateEnum.MSG.getType());
            String templateId = SmsSal.addTemplate(request.getName(), request.getSign(), makeSmsContent(request.getContent()), request.getType());
            messageTemplate.setSmsTemplateId(templateId);
        }else{
            messageTemplate = messageDal.selectById(id);
            SmsSal.updateTemplate(messageTemplate.getSmsTemplateId(), request.getName(), request.getSign(), makeSmsContent(request.getContent()), request.getType());
        }
        messageTemplate.setSmsTemplateType(Integer.parseInt(request.getType()));
        messageTemplate.setMessageSign(request.getSign());
        messageTemplate.setMessageTitle(request.getName());
        messageTemplate.setMessageContent(request.getContent());
        messageTemplate.setUpdateTime(now);
        messageTemplate.setUpdateBy(userInfo.getName());

        messageDal.inserOrUpdate(messageTemplate);

        return new EmptyResponse();
    }

    /**
     * 保持一下顺序不变，否则有问题
     * @param content
     * @return
     */
    private String makeSmsContent(String content){
        int i = 1;
        if(content.contains(MessageSal.PROJECT_NAME_TEMPLATE)){
            content = content.replace(MessageSal.PROJECT_NAME_TEMPLATE, "{" + (i++) + "}");
        }
        if(content.contains(MessageSal.PROJECT_DATE_TEMPLATE)){
            content = content.replace(MessageSal.PROJECT_DATE_TEMPLATE, "{" + (i++) + "}");
        }
        if(content.contains(MessageSal.NAME_TEMPLATE)){
            content = content.replace(MessageSal.NAME_TEMPLATE, "{" + (i++) + "}");
        }
        if(content.contains(MessageSal.SEX_TEMPLATE)){
            content = content.replace(MessageSal.SEX_TEMPLATE, "{" + (i++) + "}");
        }
        return content;
    }
}
